﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Enter.Utils
{
    public class ComposeExtend
    {
        public static List<ComposeAttribute> Headers<T>()
        {
            var pds = typeof(T).GetProperties();
            // 获取头部.
            var headers = pds.Select(p =>
            {
                var attr = p.GetCustomAttribute(typeof(ComposeAttribute)) as ComposeAttribute;
                if (attr != null)
                {
                    attr.PropertyName = p.Name;
                    attr.Property = p;
                }
                return attr;
            }).Where(p => p != null).ToList();
            return headers;
        }

        /// <summary>
        /// 使用T2的属性，T的设置
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <returns></returns>
        public static List<ComposeAttribute> Headers<T, T2>()
        {
            var target_pds = typeof(T2).GetProperties();
            var pds = typeof(T).GetProperties();
            // 获取头部.
            var headers = pds.Select(p =>
            {
                var attr = p.GetCustomAttribute(typeof(ComposeAttribute)) as ComposeAttribute;
                if (attr != null)
                {
                    attr.PropertyName = p.Name;
                    attr.Property = target_pds.Where(t => t.Name == p.Name).FirstOrDefault();
                }
                return attr;
            }).Where(p => p != null && p.Property != null).ToList();
            return headers;
        }
    }
}
